package com.study.lession.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.study.lession.contant.Constants;
import com.study.lession.domain.LessionTeacherInfo;
import com.study.lession.domain.LessionUserInfo;
import com.study.lession.service.LessionTeacherInfoService;
import com.study.lession.service.LessionUserInfoService;

@Controller
public class LoginController {
	
	@Autowired
	private LessionUserInfoService contactService;
	@Autowired
	private LessionTeacherInfoService contactServiceTeacher;

	/**
	 * 登录
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, LessionUserInfo lessionUserInfo) {
		
		List<LessionUserInfo> list = contactService.queryLessionUserInfoList(lessionUserInfo);
		
		ModelAndView view = new ModelAndView();
		if (list.size() == 1) {
			lessionUserInfo = list.get(0);
			request.getSession().setAttribute(Constants.SESSION_KEY, lessionUserInfo);
			view.addObject("msg", "【" + lessionUserInfo.getStuname() + "】登录成功");
			view.setViewName("contact/list");
		} else {
			view.addObject("msg", "账号密码错误，请重新登录");
			view.setViewName("login");
		}
		return view;
	}
	
	/**
	 * 注册
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/register")
	public ModelAndView register(HttpServletRequest request, LessionUserInfo lessionUserInfo) {
		
		int count = contactService.addContact(lessionUserInfo);
		
		ModelAndView view = new ModelAndView();
		
		if (count == 1) {
			view.addObject("msg", "【" + lessionUserInfo.getStuname() + "】注册成功");
			view.setViewName("login");
		} else {
			view.addObject("msg", "注册失败，请重新注册");
			view.setViewName("register");
		}
		return view;
	}
	

	/**
	 * 注销
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/logout")
	public ModelAndView logout(HttpServletRequest request) {
		
		ModelAndView view = new ModelAndView();		
		request.getSession().removeAttribute(Constants.SESSION_KEY);
		view.addObject("msg", "登录");
		view.setViewName("login");
		return view;
	}
	
	
	/**
	 * 登录
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/loginteacher")
	public ModelAndView loginteacher(HttpServletRequest request, LessionTeacherInfo lessionTeacherInfo) {
		
		List<LessionTeacherInfo> list = contactServiceTeacher.queryLessionTeacherInfoList(lessionTeacherInfo);
		
		ModelAndView view = new ModelAndView();
		if (list.size() == 1) {
			lessionTeacherInfo = list.get(0);
			request.getSession().setAttribute(Constants.SESSION_KEY, lessionTeacherInfo);
			view.addObject("msg", "【" + lessionTeacherInfo.getTname() + "】登录成功");
			view.setViewName("contact/listteacher");
		} else {
			view.addObject("msg", "账号密码错误，请重新登录");
			view.setViewName("loginteacher");
		}
		return view;
	}
	
	/**
	 * 注册
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/registerteacher")
	public ModelAndView registerteacher(HttpServletRequest request, LessionTeacherInfo lessionTeacherInfo) {
		
		int count = contactServiceTeacher.addContact(lessionTeacherInfo);
		
		ModelAndView view = new ModelAndView();
		
		if (count == 1) {
			view.addObject("msg", "【" + lessionTeacherInfo.getTname() + "】注册成功");
			view.setViewName("loginteacher");
		} else {
			view.addObject("msg", "注册失败，请重新注册");
			view.setViewName("registerteacher");
		}
		return view;
	}
	

	/**
	 * 注销
	 * @param lessionUserInfo
	 * @return
	 */
	@RequestMapping("/logoutteacher")
	public ModelAndView logoutteacher(HttpServletRequest request) {
		
		ModelAndView view = new ModelAndView();		
		request.getSession().removeAttribute(Constants.SESSION_KEY);
		view.addObject("msg", "登录");
		view.setViewName("loginteacher");
		return view;
	}
}
